
// const base = 'http://localhost:8800'
const base = 'http://127.0.0.1:4523/m1/4426881-4072185-default';

/**
 * @typedef {
 *     {
 *         host: string
 *     }
 * } scanStratReq
 *
 * @typedef {
 *     {
 *         taskid: number
 *     }
 * } liveHostReq
 *
 *
 *
 * @typedef {
 *     {
 *         taskid: number
 *         ip: string
 *         taskStartTime: string
 *     }
 * } HistoryData
 *
 * @typedef {
 *     {
 *         ip: string
 *         ports: number[]
 *     }
 * } liveHostData
 *
 * @typedef {
 *     {
 *         ip: string
 *         vul: string[]
 *     }
 * } VulData
 *
 * */

/**
 *
 * @template T
 * @typedef {
 *     {
 *         Status: string
 *         Message: string
 *         Result: T
 *     }
 * } Resp
 * */

/**
 * @param { scanStratReq } data
 * */
export function StartScan(data) {
    return fetch(`${base}/api/scan/start`, {
        method: "POST",
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<any>} res */
            let data = res;
            if (data.Status !== "1") {
                ElMessage.error(data.Message)
                throw data.Message
            }

            return data.Message
        })

}

export function StopScan() {
    return fetch(`${base}/api/scan/stop`, {
        method: "POST",
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<any>} res */
            let data = res;
            if (data.Status !== "1") {
                ElMessage.error(data.Message)
                throw data.Message
            }

            return data.Message
        })

}

export function ScanStatus() {
    return fetch(`${base}/api/scan/status`, {
        method: "GET",
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<any>} res */
            let data = res;
            return data.Status
        })

}

/**
 * @return {Promise<HistoryData[]>}
 * */
export function ScanHistory() {
    return fetch(`${base}/api/scan/history`, {
        method: "GET",
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<HistoryData[]>} res */
            let data = res;

            if (data.Status !== "1") {
                

                ElMessage.error(data.Message)
                throw data.Message
            }

            return data.Result
        })

}

/**
 * @param { liveHostReq } data
 * @return { Promise<liveHostData[]> }
 * */
export function ScanLiveHosts(data) {
    return fetch(`${base}/api/scan/liveHosts?taskid=${data.taskid}`, {
        method: "GET",
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<Object>} res */
            let data = res;
            if (data.Status !== "1") {
                ElMessage.error(data.Message)
                throw data.Message
            }

            /**@type { liveHostData[] } resp*/
            let resp = []

            Object.keys(data.Result).forEach(value => {
                /**@type { string[] }  */
                let vulStrs = data.Result[value]

                resp.push({
                    ip: value,
                    ports: vulStrs
                })

            })

            return resp
        })

}

/** @typedef {
 *    {
 *        taskid: number
 *    }
 *} VulInfoReq
 * */

/**
 * @param { VulInfoReq } data
 * @return {Promise<VulData[]>}
 * */
export function ScanVulInfo(data) {
    return fetch(`${base}/api/vul/info?taskid=${data.taskid}`, {
        method: "GET",
    })
        .then(res => res.json())
        .then(res => {
            /**@type {Resp<Object>} res */
            let data = res;
            if (data.Status !== "1") {
                ElMessage.error(data.Message)
                throw data.Message
            }

            /**@type { VulData[] } resp*/
            let resp = []

            Object.keys(data.Result).forEach(value => {
                /**@type { string[] }  */
                let vulStr = data.Result[value]

                resp.push({
                    ip: value,
                    vul: vulStr
                })

            })

            return resp
        })

}

/**
 *
 * @return {EventSource}
 * */
export function ScanLogSSE() {
    return new EventSource(`${base}/api/scan/info`)
}